//
//  IGSimilarArtVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/16.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGSimilarArtVC.h"
#import "FindLayout.h"
#import "IGGetCollectApi.h"
#import "IGMydeletApi.h"
#import "IGiGalleryListApi.h"
#import "MoreArteditCell.h"
#import "IGPushiGalleryListVC.h"
#import "IGiGalleryPushApi.h"
#import "IGMoreWorksSelectVC.h"
#import "IGWorkDetailRecApi.h"
#import "XiangQingViewController.h"

@interface IGSimilarArtVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
{
    UICollectionView *collect;
    NSString *uId;//用户id
    UIButton *rightBtn;
    NSString *artId;
    
}
@property(nonatomic,strong)IGWorkDetailRecApi *recPictureApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation IGSimilarArtVC
#pragma mark-懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor mainBackgroundColor];
    self.title=@"相似画作";
    [self setRightButton];
    [self setCollectionview];
    self.recPictureApi = [IGWorkDetailRecApi new];
      [self requestDate];
}
//设置导航栏右侧按钮
-(void)setRightButton{
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 36);
    rightBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 0, 0, -10);
    [rightBtn.titleLabel setFont:[UIFont font16]];
    [rightBtn setTitle:@"多选" forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"多选"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor c4Color]forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(moreSelect) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
//初始化collectionview
-(void)setCollectionview{
    FindLayout *layout = [[FindLayout alloc] init];
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, KSCREENWIDTH, KSCREENHEIGHT-NavigationMagin) collectionViewLayout:layout];
    collect.backgroundColor = [UIColor clearColor];
    collect.delegate = self;
    collect.dataSource = self;
    if (@available(iOS 11.0, *)) {
        collect.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        collect.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
        collect.scrollIndicatorInsets = collect.contentInset;
    }
    collect.alwaysBounceVertical = YES;
    [collect registerClass:[MoreArteditCell class] forCellWithReuseIdentifier:@"MoreArteditCell"];
    [self.view addSubview:collect];
}
-(void)requestDate{
    self.recPictureApi.pid=self.pictureId;
    [self.recPictureApi startWithDecodeClass:[IGWorkDetailRecResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGWorkDetailRecResp *resp=responseObject;
            IGWorkDetailRecModel *model=resp.data;
            if (model.productList&&model.productList.count>0) {
        
            }
            else
            {
                [collect ig_endFooterRefreshingWithNoMoreData];
            }
            _dataArr = [NSMutableArray arrayWithArray:model.productList];
            [collect reloadData];
        }
    }];
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.dataArr.count>0?self.dataArr.count:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MoreArteditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreArteditCell" forIndexPath:indexPath];
    cell.data=_dataArr[indexPath.item];
    cell.isSimilar=YES;
    [cell setNeedsLayout];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    IGWorkDetailSigleRecModel *info=_dataArr[indexPath.item];
    XiangQingViewController *vc=[[XiangQingViewController alloc]init];
    vc.p_id=info.pid;
    [UIViewController ig_pushViewController:vc animated:YES];
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    IGWorkDetailSigleRecModel *base =_dataArr[indexPath.item];
    if ([base.plates integerValue]==1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


-(void)moreSelect{
    IGMoreWorksSelectVC *vc=[[IGMoreWorksSelectVC alloc]init];
    vc.recDataArr=_dataArr;
    [UIViewController ig_pushViewController:vc animated:YES];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
